//
//  NetRequest.m
//  Molian
//
//  Created by xiaocankeji on 16/4/29.
//  Copyright © 2016年 xiaocan. All rights reserved.
//

#import "NetRequest.h"
#import <AFNetworking/AFNetworking.h>

@implementation NetRequest



//xml  请求
- (void)SOAPData:(NSString *)url soapBody:(NSDictionary *)dic success:(finish)success failure:(fail)fail{
    self.finish = success;
    self.fail = fail;
    NSString *requestMsStr = @"";
    for (NSString *parameterkey in dic) {
        NSString *parametervalue =[dic objectForKey:parameterkey];
        NSString *parameter =[NSString stringWithFormat:@"<%@>%@</%@>\n",parameterkey,parametervalue,parameterkey];
        requestMsStr=[requestMsStr stringByAppendingString:parameter];
    }
    NSString *soapStr = [NSString stringWithFormat:@"<xml>%@</xml>",requestMsStr];
    
  //  NSString *soapStr = [NSString stringWithFormat:@"<xml><appid>wx0d2d47e2b4c7ed74</appid><body>会员支付</body><mch_id>1335947201</mch_id><nonce_str>101280101</nonce_str><notify_url>http://www.wudutuan.com/member/pay/wxnotify</notify_url><out_trade_no>12345</out_trade_no><spbill_create_ip>192.168.0.1</spbill_create_ip><total_fee>1</total_fee><trade_type>APP</trade_type><sign>90F1F8A15B559D82EC0DA6270F37948C</sign></xml>"];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    
    // 设置请求超时时间
    manager.requestSerializer.timeoutInterval = 30;
    
    // 返回NSData
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 设置请求头，也可以不设置
    [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"];
    
    // 设置HTTPBody
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
        return soapStr;
    }];
    
    
    
    
    [manager POST:url parameters:soapStr progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.finish(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        self.fail(error);
    }];
}



@end
